package com.zy.experience_system.service.impl;

import com.zy.experience_system.dto.StuHomeworkDTO;
import com.zy.experience_system.entity.*;
import com.zy.experience_system.enums.ResultEnum;
import com.zy.experience_system.exception.CommonException;
import com.zy.experience_system.form.*;
import com.zy.experience_system.repository.*;
import com.zy.experience_system.service.StuHomeworkService;
import com.zy.experience_system.util.MyDateUtil;
import com.zy.experience_system.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class StuHomeworkServiceImpl implements StuHomeworkService {

    @Autowired
    private StuHomeworkRepository stuHomeworkRepository;
    @Autowired
    private TestAndSetRepository testAndSetRepository;
    @Autowired
    private TestInformationRepository testInformationRepository;
    @Autowired
    private TestSetRepository testSetRepository;
    @Autowired
    private StuInformationRepository stuInformationRepository;
    @Autowired
    private TeachInformationRepository teachInformationRepository;
    @Autowired
    private StuAndSetRepository stuAndSetRepository;


    @Override
    public void save(StuHomeworkDTO stuHomeworkDTO) {
        Integer homeworkId = stuHomeworkDTO.getHomeworkId();
        StuHomework stuHomework = new StuHomework();
        if (homeworkId!=null){
            stuHomeworkRepository.findById(homeworkId).get();
        }
        BeanUtils.copyProperties(stuHomeworkDTO,stuHomework);
        stuHomeworkRepository.save(stuHomework);
    }


    @Override
    public void save(SetAndStuForm setAndStuForm) {
        Integer setId = setAndStuForm.getSetId();
        String stuId = setAndStuForm.getStuId();
        if (testSetRepository.findById(setId).isPresent()&&
                stuInformationRepository.findAllByStuId(stuId).size()!=0) {
            StuAndSet stuAndSet = stuAndSetRepository.findByStuIdAndSetId(stuId,setId);
            stuAndSet.setOpen("yes");
            stuAndSetRepository.save(stuAndSet);
            List<TestAndSet> testAndSetList = testAndSetRepository.findAllBySetId(setId);
            if (testAndSetList.size() != 0) {
                for (TestAndSet testAndSet : testAndSetList) {
                    Integer testId = testAndSet.getTestId();
                    TestInformation testInformation = testInformationRepository.findById(testId).get();
                    String testName = testInformation.getTestName();
                    String testType = testInformation.getTestType();
                    TestSet testSet = testSetRepository.findById(setId).get();
                    String setName = testSet.getSetName();
                    StuInformation stuInformation = stuInformationRepository.findByStuId(stuId);
                    String stuName = stuInformation.getStuName();
                    StuHomework stuHomework = new StuHomework();
                    stuHomework.setStuId(stuId);
                    stuHomework.setStuName(stuName);
                    stuHomework.setTestId(testId);
                    stuHomework.setTestName(testName);
                    stuHomework.setTestType(testType);
                    stuHomework.setSetId(setId);
                    stuHomework.setSetName(setName);
                    stuHomeworkRepository.save(stuHomework);
                }
                return;
            }
            throw new CommonException(ResultEnum.FAIL);
        }
        throw new CommonException(ResultEnum.FAIL);
    }

    @Override
    public List<HomeworkSimpleVO> findAllByStuIdAndSetId(SetAndStuForm setAndStuForm) {
        Integer setId = setAndStuForm.getSetId();
        String stuId = setAndStuForm.getStuId();
        if (stuHomeworkRepository.findAllByStuIdAndSetId(stuId,setId).size()!=0){
            List<HomeworkSimpleVO> homeworkSimpleVOList = new ArrayList<>();
            List<StuHomework> stuHomeworkList = stuHomeworkRepository.findAllByStuIdAndSetId(stuId,setId);
            for (StuHomework stuHomework : stuHomeworkList) {
                HomeworkSimpleVO homeworkSimpleVO = new HomeworkSimpleVO();
                BeanUtils.copyProperties(stuHomework,homeworkSimpleVO);
                homeworkSimpleVOList.add(homeworkSimpleVO);
            }
            return homeworkSimpleVOList;
        }
        throw new CommonException(ResultEnum.FAIL);
    }

    @Override
    public void saveScore(CheckHomeworkForm checkHomeworkForm) {
        Integer homeworkId = checkHomeworkForm.getId();
        if (stuHomeworkRepository.findById(homeworkId).isPresent()){
            StuHomework homework = stuHomeworkRepository.findById(homeworkId).get();
            String stuId = homework.getStuId();
            Integer setId = homework.getSetId();
            Integer score = checkHomeworkForm.getScore();
            String comment = checkHomeworkForm.getComment();
            StuHomework stuHomework = stuHomeworkRepository.findById(homeworkId).get();
            stuHomework.setScore(score);
            stuHomework.setComment(comment);
            stuHomework.setFinish("done");
            stuHomeworkRepository.save(stuHomework);
            StuAndSet stuAndSet = stuAndSetRepository.findByStuIdAndSetId(stuId,setId);
            Integer totalScore = stuAndSet.getTotalScore();
            totalScore += score;
            stuAndSet.setTotalScore(totalScore);
            stuAndSetRepository.save(stuAndSet);
            return;
        }
        throw new CommonException(ResultEnum.FAIL);
    }

    @Override
    public StuHomeworkVO findById(Integer homeworkId) {
        if (stuHomeworkRepository.findById(homeworkId).isPresent()){
            StuHomework stuHomework = stuHomeworkRepository.findById(homeworkId).get();
            Integer testId = stuHomework.getTestId();
            if (testInformationRepository.findById(testId).isPresent()){
                TestInformation testInformation = testInformationRepository.findById(testId).get();
                StuHomeworkVO stuHomeworkVO = new StuHomeworkVO();
                TestVO testVO = new TestVO();
                HomeworkAllVO homeworkAllVO = new HomeworkAllVO();
                BeanUtils.copyProperties(testInformation,testVO);
                testVO.setCreateTime(MyDateUtil.date2String(testInformation.getCreateTime()));
                testVO.setUpdateTime(MyDateUtil.date2String(testInformation.getUpdateTime()));
                stuHomeworkVO.setTestVO(testVO);
                BeanUtils.copyProperties(stuHomework,homeworkAllVO);
                stuHomeworkVO.setHomeworkAllVO(homeworkAllVO);
                return stuHomeworkVO;
            }
            throw new CommonException(ResultEnum.FAIL);
        }
        throw new CommonException(ResultEnum.FAIL);
    }


    @Override
    public TeachHomeworkVO findByTestIdAndSetId(TestAndSetNoTeachForm testAndSetNoTeachForm) {
        Integer setId = testAndSetNoTeachForm.getSetId();
        Integer testId = testAndSetNoTeachForm.getTestId();
        if (testInformationRepository.findById(testId).isPresent()&&testSetRepository.findById(setId).isPresent()){
            TestInformation testInformation = testInformationRepository.findById(testId).get();
            TestVO testVO = new TestVO();
            TeachHomeworkVO teachHomeworkVO = new TeachHomeworkVO();
            BeanUtils.copyProperties(testInformation,testVO);
            testVO.setCreateTime(MyDateUtil.date2String(testInformation.getCreateTime()));
            testVO.setUpdateTime(MyDateUtil.date2String(testInformation.getUpdateTime()));
            teachHomeworkVO.setTestVO(testVO);
            List<StuHomework> stuHomeworkList = stuHomeworkRepository.findAllByTestIdAndSetId(testId, setId);
            List<HomeworkAllVO> homeworkAllVOList = new ArrayList<>();
            for (StuHomework stuHomework : stuHomeworkList) {
                HomeworkAllVO homeworkAllVO = new HomeworkAllVO();
                BeanUtils.copyProperties(stuHomework,homeworkAllVO);
                homeworkAllVOList.add(homeworkAllVO);
            }
            teachHomeworkVO.setHomeworkAllVOList(homeworkAllVOList);
            return teachHomeworkVO;
        }
        throw new CommonException(ResultEnum.FAIL);
    }

    @Override
    public void saveAnswer(HomeworkAnswerForm homeworkAnswerForm) {
        Integer homeworkId = homeworkAnswerForm.getId();
        if (stuHomeworkRepository.findById(homeworkId).isPresent()){
            StuHomework stuHomework = stuHomeworkRepository.findById(homeworkId).get();
            BeanUtils.copyProperties(homeworkAnswerForm,stuHomework);
            stuHomework.setFinish("yes");
            stuHomeworkRepository.save(stuHomework);
            return;
        }
        throw new CommonException(ResultEnum.FAIL);
    }

    @Override
    public List<TeachTestListVO> findBySetId(Integer setId) {
        if (testSetRepository.findById(setId).isPresent()){
            List<TestAndSet> testAndSetList = testAndSetRepository.findAllBySetId(setId);
            List<TeachTestListVO> teachTestListVOList = new ArrayList<>();
            for (TestAndSet testAndSet : testAndSetList) {
                TeachTestListVO teachTestListVO = new TeachTestListVO();
                Integer testId = testAndSet.getTestId();
                TestInformation testInformation = testInformationRepository.findById(testId).get();
                TeachTestSimpleVO teachTestSimpleVO = new TeachTestSimpleVO();
                BeanUtils.copyProperties(testInformation,teachTestSimpleVO);
                teachTestListVO.setTeachTestSimpleVO(teachTestSimpleVO);
                Integer yes = stuHomeworkRepository.countAllByTestIdAndSetIdAndFinish(testId,setId,"yes");
                Integer done = stuHomeworkRepository.countAllByTestIdAndSetIdAndFinish(testId,setId,"done");
                Integer all = yes + done;
                HomeworkCountVO homeworkCountVO = new HomeworkCountVO();
                homeworkCountVO.setAll(all);
                homeworkCountVO.setDone(done);
                homeworkCountVO.setYes(yes);
                teachTestListVO.setHomeworkCountVO(homeworkCountVO);
                teachTestListVOList.add(teachTestListVO);
            }
            return teachTestListVOList;
        }
        throw new CommonException(ResultEnum.FAIL);
    }

    @Override
    public List<StuOrderVO> findOrderBySetId(Integer setId) {
        if (stuAndSetRepository.findAllBySetIdAndOpenOrderByTotalScoreDesc(setId,"yes").size()!=0) {
            List<StuOrderVO> stuOrderVOList = new ArrayList<>();
            List<StuAndSet> stuAndSetList = stuAndSetRepository.findAllBySetIdAndOpenOrderByTotalScoreDesc(setId,"yes");
            Integer order = 1;
            TestSet testSet = testSetRepository.findById(setId).get();
            String teachId = testSet.getTeachId();
            for (StuAndSet stuAndSet : stuAndSetList) {
                if (stuAndSet.getStuId().equals(teachId)){
                    continue;
                }
                String stuId = stuAndSet.getStuId();
                Integer totalScore = stuAndSet.getTotalScore();
                StuInformation stuInformation = stuInformationRepository.findByStuId(stuId);
                String stuName = stuInformation.getStuName();
                StuOrderInfoVO stuOrderInfoVO = new StuOrderInfoVO();
                stuOrderInfoVO.setId(stuId);
                stuOrderInfoVO.setName(stuName);
                stuOrderInfoVO.setTotalScore(totalScore);
                stuOrderInfoVO.setOrder(order);
                order++;
                StuOrderVO stuOrderVO = new StuOrderVO();
                stuOrderVO.setStuOrderInfoVO(stuOrderInfoVO);
                List<TestOrderVO> testOrderVOList = new ArrayList<>();
                List<StuHomework> stuHomeworkList = stuHomeworkRepository.findAllByStuIdAndSetId(stuId,setId);
                if (stuHomeworkList.size()!=0){
                    for (StuHomework stuHomework : stuHomeworkList) {
                        TestOrderVO testOrderVO = new TestOrderVO();
                        BeanUtils.copyProperties(stuHomework,testOrderVO);
                        testOrderVOList.add(testOrderVO);
                    }
                    stuOrderVO.setTestOrderVOList(testOrderVOList);
                    stuOrderVOList.add(stuOrderVO);
                }
            }
            return stuOrderVOList;
        }
        throw new CommonException(ResultEnum.FAIL);
    }
}
